package com.aoxin.controller.opm;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.aoxin.mapper.SlfChildExpectedTimeMapper;
import com.aoxin.model.course.SlfChildren;
import com.aoxin.model.opm.SlfUser;
import com.aoxin.service.inter.course.webCourse.ChildrenService;
import com.aoxin.service.inter.opm.UserService;
import com.aoxin.util.AjaxRes;
import com.aoxin.util.Const;
import com.aoxin.util.RedisUtil;
import com.aoxin.util.ThreadLocalUtil;
import com.aoxin.util.enumerate.OperateType;
import com.aoxin.util.enumerate.UserType;
import com.aoxin.util.log.LogInfo;

/**
 * 用户 Controller
 * 
 * @author Melody
 * @date 2018年4月1日 上午11:59:11
 */
@Controller
@ResponseBody
@RequestMapping("/web/user")
public class UserController {

	@Autowired
	private ChildrenService childrenService;

	@Autowired
	private UserService userService;

	@Autowired
	private SlfChildExpectedTimeMapper slfChildExpectedTimeMapper;

	/**
	 * 查询当前登录用户
	 * 
	 */
	@RequestMapping("/getUer")
	public AjaxRes getUer(AjaxRes ajaxRes) {
		try {
			SlfUser user = ThreadLocalUtil.getUser();
			ajaxRes.setSucceed(user, Const.DEL_SUCCEED);
		} catch (Exception e) {
			ajaxRes.setFailMsg(e.getMessage());
		}
		return ajaxRes;
	}

	/**
	 * 查询用户的当前选的孩子
	 */
	@RequestMapping("/getChildren")
	public AjaxRes getChildren(AjaxRes ajaxRes) {
		try {
			SlfUser user = ThreadLocalUtil.getUser();
			SlfChildren slfChildren = (SlfChildren) RedisUtil.get(user.getPhone() + user.getUserId() + ChildrenService.REDIS_CHILDREN_CODE);
			ajaxRes.setSucceed(slfChildren, Const.DEL_SUCCEED);
		} catch (Exception e) {
			ajaxRes.setFailMsg(e.getMessage());
		}
		return ajaxRes;
	}

	/**
	 * 切换孩子
	 * 
	 * @param json
	 *            孩子ID
	 */
	@RequestMapping("/setChildren")
	public AjaxRes setChildren( AjaxRes ajaxRes,@RequestParam("json") String json) {
		System.out.println("setChildren");
		try {
			//JSONObject jsonObject = JSONObject.fromObject(json);
			//Integer childrenId = Integer.valueOf(jsonObject.get("json").toString());
			Integer childrenId = Integer.valueOf(json);
			System.out.println("param:" + childrenId);
			SlfChildren slfChildren = this.childrenService.getAChildren(childrenId);
			SlfUser user = ThreadLocalUtil.getUser();
			Integer userId = user.getUserId();
			SlfChildren oldslfChildren = (SlfChildren) RedisUtil.get(user.getPhone() + user.getUserId() + ChildrenService.REDIS_CHILDREN_CODE);
			if (null != oldslfChildren) {
				RedisUtil.del(user.getPhone() + user.getUserId() + ChildrenService.REDIS_CHILDREN_CODE);
			}
			childrenService.updateDefaultChildren(childrenId, userId);
			childrenService.recoverChildren(childrenId, userId);
			RedisUtil.set(user.getPhone() + user.getUserId() + ChildrenService.REDIS_CHILDREN_CODE, slfChildren);
			ajaxRes.setSucceedMsg(Const.DEL_SUCCEED);
			ajaxRes.setSucceedMsg("yes");
		} catch (Exception e) {
			ajaxRes.setFailMsg(e.getMessage());
		}
		return ajaxRes;
	}

	@RequestMapping("/getChildrens")
	@LogInfo(content = "获取所有孩子", operateType = OperateType.QUERY, userType = UserType.USER)
	public AjaxRes getChildrens(AjaxRes ajaxRes) {
		try {
			List<SlfChildren> slfChildrens = this.childrenService.getChildrenInfo(ThreadLocalUtil.getUser().getUserId());
			ajaxRes.setSucceed(slfChildrens, Const.DATA_SUCCEED);
		} catch (Exception e) {
			ajaxRes.setFailMsg(e.getMessage());
		}
		return ajaxRes;
	}

	/**
	 * 查询所有用户
	 * @param ajaxRes
	 * @return
	 */
	@RequestMapping("/getUserList")
	public AjaxRes getUserList(AjaxRes ajaxRes,SlfUser slfUser){
		try {
			List<SlfUser> slfUserList = this.userService.getUserList(slfUser);
			ajaxRes.setSucceed(slfUserList,Const.DATA_SUCCEED);
		}catch (Exception e){
			ajaxRes.setFailMsg(e.getMessage());
		}
		return ajaxRes;
	}
	
	
	/**
	 * 添加用户立方币（管理员功能）
	 * @param ajaxRes
	 * @param slfUser
	 * @return
	 */
	@RequestMapping("/updateUserLfb")
	public AjaxRes updateUserLfb(AjaxRes ajaxRes,SlfUser slfUser){
		try {
			this.userService.updateUserLfb(slfUser);
			ajaxRes.setSucceed(Const.UPDATE_SUCCEED);
		}catch (Exception e){
			ajaxRes.setFailMsg(e.getMessage());
		}
		return ajaxRes;
	}
		
	
	
	
}
